<template>
	<view class="container">
		<!-- 搜索筛选区域 -->
		<view class="search-container">
			<view class="search-row">
				<view class="search-item  user-search">
					<text class="search-label">收运员:</text>
					<uni-easyinput v-model="searchData.userName" placeholder="请输入收运员" />
				</view>
			</view>
			<view class="search-row">
				<uni-datetime-picker v-model="collectionDate" type="datetimerange" rangeSeparator="至" />
			</view>
			<view class="search-btn-group">
				<button class="search-btn" @click="handleSearch">搜索</button>
				<button class="reset-btn" @click="handleReset">重置</button>
			</view>
		</view>
		<view class="record-item">
			<view class="record-header">
				<text class="merchant-id">合计</text>
				<text class="merchant-id" >收运次数{{ recordAmountData.recordNum }} | 商户数量:{{ recordAmountData.merchantNum }}</text>
			</view>
			<view class="record-content">
				<view class="field-row">
					<text class="label">地上油收油量:</text>
					<text class="value">{{ recordAmountData.groundQuantity }} 公斤</text>
				</view>
				<view class="field-row">
					<text class="label">地上油单价:</text>
					<text class="value">{{ recordAmountData.groundPrice }} 元/公斤</text>
				</view>
				<view class="field-row">
					<text class="label">地下油收油量:</text>
					<text class="value">{{ recordAmountData.undergroundQuantity }} 公斤</text>
				</view>
				<view class="field-row">
					<text class="label">灶台油收油量:</text>
					<text class="value">{{ recordAmountData.stoveQuantity }} 公斤</text>
				</view>
			</view>
		</view>
		<!-- 分页组件 -->
		<uni-pagination class="pagination" :current="pageNum" :total="total" :pageSize="pageSize"
			@change="handlePageChange" />

		<scroll-view class="scroll-container" scroll-y style="height: calc(100vh - 160rpx)">
			<view v-for="record in records" :key="record.userId" class="record-item">
				<view class="record-header">
					<text class="merchant-id">收运员: {{ record.userName }}</text>
<!-- 					<text class="merchant-id" @click="scanQRcodes(record.userName)">收运次数{{ record.recordNum }} | 商户数量: {{ record.merchantNum }}</text> -->
					<view class="action-btns">
						<button class="edit-btn" @click="scanQRcodes(record.userName)">收运记录</button>
						<!-- <button class="delete-btn" @click="delReport(item.id)">删除</button> -->
					</view>
				</view>
				<view class="record-content">
					<!--          <view class="field-row">-->
					<!--            <text class="label">收运时间:</text>-->
					<!--            <text class="value">{{ formatDateDay(record.collectionDate) }}</text>-->
					<!--          </view>-->
					<view class="field-row">
						<text class="label">收运次数:</text>
						<text class="value">{{ record.recordNum }} </text>
					</view>
					<view class="field-row">
						<text class="label">商户数量:</text>
						<text class="value">{{ record.merchantNum }} </text>
					</view>
					<view class="field-row">
						<text class="label">地上油收油量:</text>
						<text class="value">{{ record.groundQuantity }} 公斤</text>
					</view>
					<view class="field-row">
						<text class="label">地上油单价:</text>
						<text class="value">{{ record.groundPrice }} 元/公斤</text>
					</view>
					<view class="field-row">
						<text class="label">地下油收油量:</text>
						<text class="value">{{ record.undergroundQuantity }} 公斤</text>
					</view>
					<view class="field-row">
						<text class="label">灶台油收油量:</text>
						<text class="value">{{ record.stoveQuantity }} 公斤</text>
					</view>
				</view>
			</view>
		</scroll-view>


	</view>
</template>

<script>
	import {
		recordDay,
		recordAmountTo
	} from '@/api/system/record.js';

	export default {
		//name: "收运统计",
		data() {
			return {
				pageNum: 1,
				pageSize: 10,
				total: 0,
				records: [],
				searchData: {
					userName: '', // 收运员名称
					collectionDateStart: new Date().toISOString().slice(0, 10) + " 00:00:00", // 开始日期
					collectionDateEnd: new Date().toISOString().slice(0,10) + " 23:59:59" // 结束日期
				},
				collectionDate: [new Date().toISOString().slice(0, 10) + " 00:00:00", new Date().toISOString().slice(0,
					10) + " 23:59:59"
				],
				recordAmountData: {}
			};
		},
		onLoad(options) {
			// 加载数据
			this.fetchData();
			this.fetchAmountData();
		},
		onShow(options) {
			this.fetchData();
			this.fetchAmountData();
		},
		methods: {
			// 加载数据
			async fetchData() {
				try {
					const res = await recordDay({
						'pageNum': this.pageNum,
						'pageSize': this.pageSize,
						...this.searchData
					});
					this.records = res.rows;
					this.total = res.total;
				} catch (error) {
					uni.showToast({
						title: '加载失败',
						icon: 'none'
					});
				}
			},
			// 合计
			async fetchAmountData() {
				try {
					const res = await recordAmountTo({
						'pageNum': this.pageNum,
						'pageSize': this.pageSize,
						...this.searchData
					});
					this.recordAmountData = res.data;
				} catch (error) {
					uni.showToast({
						title: '加载失败',
						icon: 'none'
					});
				}
			},
			// 翻页
			handlePageChange(e) {
				this.pageNum = e.current;
				this.fetchData();
			},
			// 搜索
			handleSearch() {
				console.log(this.searchData)
				this.searchData.collectionDateStart = this.collectionDate[0]
				this.searchData.collectionDateEnd = this.collectionDate[1]
				// 重置页码
				this.pageNum = 1;
				this.fetchData();
				this.fetchAmountData();
			},
			// 重置搜索条件
			handleReset() {
				this.searchData = {
					userName: '',
					collectionDateStart: new Date().toISOString().slice(0, 10) + " 00:00:00",
					collectionDateEnd: new Date().toISOString().slice(0, 10) + " 23:59:59"
				};
				// 重置页码并重新加载数据
				this.pageNum = 1;
				this.fetchData();
				this.fetchAmountData();
			},
			formatDateDay(value) {
				const data = new Date(value);
				var month = data.getMonth() + 1;
				var day = data.getDate();
				const year = data.getFullYear();

				if (month < 10) month = "0" + month;
				if (day < 10) day = "0" + day;

				const formattedTime = `${year}-${month}-${day}`;
				return formattedTime;
			},
			// 跳转收油，同扫码
			scanQRcodes(userName) {
				console.log(userName)
				uni.navigateTo({
					url: `../record/record2record?userName=${userName}`
				});
			},
		}
	}
</script>



<style scoped>
	.container {
		padding: 20rpx;
		background-color: #f5f5f5;
	}

	.toolbar {
		/* margin-bottom: 20rpx; */
	}

	.add-btn {}

	.pagination {
		margin: 20rpx 0;
	}

	.scroll-container {
		border-radius: 12rpx;
		background-color: white;
		overflow: hidden;
	}

	.record-item {
		margin: 10rpx 0;
		padding: 20rpx;
		background-color: white;
		border-radius: 12rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.record-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
		border-bottom: 1rpx solid #e5e5e5;
		padding-bottom: 10rpx;
	}

	.merchant-id {
		font-size: 32rpx;
		color: #333;
		font-weight: bold;
		width: 50%;
	}

	.action-btns {
		display: flex;
		gap: 10rpx;
	}

	.edit-btn {
		background-color: #55B3FF;
		color: white;
		border-radius: 8rpx;
		padding: 8rpx 16rpx;
		font-size: 24rpx;
	}

	.delete-btn {
		background-color: #FF6666;
		color: white;
		border-radius: 8rpx;
		padding: 8rpx 16rpx;
		font-size: 24rpx;
	}

	.field-row {
		display: flex;
		align-items: center;
		margin-bottom: 12rpx;
	}

	.label {
		width: 300rpx;
		color: #666;
		font-weight: bold;
	}

	.value {
		flex: 1;
		color: #333;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	/* 响应式布局 */
	@media (min-width: 750rpx) {
		.scroll-container {
			width: 70%;
			margin: 0 auto;
		}
	}

	.search-container {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.search-row {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
		margin-bottom: 20rpx;
	}

	.search-item {
		display: flex;
		align-items: center;
		width: 100%;
	}

	.search-label {
		display: inline-block;
		width: 100rpx;
		margin-right: 10rpx;
		color: #666;
	}

	.search-btn-group {
		display: flex;
		justify-content: center;
		gap: 30rpx;
	}

	.search-btn,
	.reset-btn {
		width: 200rpx;
		height: 70rpx;
		line-height: 70rpx;
		border-radius: 10rpx;
		color: #fff;
		font-size: 28rpx;
	}

	.search-btn {
		background-color: #1989fa;
	}

	.reset-btn {
		background-color: #999;
	}

	.user-search {
		flex: 1;
	}
</style>